﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.BusinessLogicLayer;
public partial class NewDeviceAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("login.aspx");
            }
            /*进入本信息添加页显示无图的图片*/
            //this.DeviceImage.ImageUrl = "Images\\NoImage.jpg";
        }
    }

    ///*上传设备图片*/
    //protected void Btn_Upload_Click(object sender, EventArgs e)
    //{
    //    /*如果用户上传了文件*/
    //    if(this.DeviceImageUpload.PostedFile.ContentLength > 0)
    //    {
    //        /*验证上传的文件格式，只能为gif和jpeg格式*/
    //        string mimeType = this.DeviceImageUpload.PostedFile.ContentType;
    //        if(String.Compare(mimeType,"image/gif",true) == 0 || String.Compare(mimeType,"image/pjpeg",true) == 0)
    //        {
    //            this.DeviceImagePath.Text = "上传文件中....";
    //            string extFileString = System.IO.Path.GetExtension(this.DeviceImageUpload.PostedFile.FileName); /*获取文件扩展名*/
    //            string saveFileName = this.MakeFileName(extFileString); /*根据扩展名生成文件名*/
    //            string imagePath = "deviceImg\\" + saveFileName;/*图片路径*/
    //            this.DeviceImageUpload.PostedFile.SaveAs(Server.MapPath(imagePath));
    //            this.DeviceImage.ImageUrl = imagePath;
    //            this.DeviceImagePath.Text = imagePath;
    //        }
    //        else
    //        {
    //            Response.Write("<script>alert('上传文件格式不正确!');</script>");
    //        }
    //    }
    //}

    /*添加设备信息*/
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        /*建立一个设备对象，然后传入各个字段的属性值，最后调用插入函数执行添加*/
        DeviceInfo device = new DeviceInfo();
        device.DeviceName = this.DeviceName.Text;
        device.DeviceTypeId = Int32.Parse(this.DeviceType.SelectedValue);
        //device.DeviceSign = this.DeviceSign.Text;
        device.DeviceModel = this.DeviceModel.Text;
        device.DeviceSerialNumber = this.DeviceSerialNumber.Text;
        //device.DeviceImagePath = this.DeviceImagePath.Text;
        device.DeviceUser = this.DeviceUser.Text;
        device.DepartmentName = this.DeviceDpt.SelectedValue;
        device.DeviceCPU = this.txtCPU.Text;
        device.DeviceHarddisk = this.txtHardisk.Text;
        device.DeviceMemory = this.txtMemory.Text;
        device.IPAddress = this.txtIPAddress.Text;
        device.WindowNumber = txtWindowNumber.Text;
        //if(this.DeviceOutDate.Text != "")
        //    device.DeviceOutDate = Convert.ToDateTime(this.DeviceOutDate.Text);
        //device.DevicePurchaseTime = Convert.ToDateTime(this.DevicePurchaseTime.Text);
        device.DeviceMemo = this.DeviceMemo.Text;

        /*插入函数NewDeviceAdd返回添加是否成功，如果失败，ErrMessage中保存了错误信息*/
        if (device.NewDeviceAdd())
            Response.Write("<script>alert('新设备信息添加成功!');location.href='NewDeviceAdd.aspx';</script>");
        else
            Response.Write("<script>alert('" + device.ErrMessage + "');</script>");

    }

    /*根据当前系统时间生成一个文件名*/
    private string MakeFileName(string exeFileString)
    {
        System.DateTime now = System.DateTime.Now;
        int year = now.Year;
        int month = now.Month;
        int day = now.Day;
        int hour = now.Hour;
        int minute = now.Minute;
        int second = now.Second;

        string yearString = year.ToString();
        string monthString = month < 10 ? ("0" + month) : month.ToString();
        string dayString = day < 10 ? ("0" + day) : day.ToString();
        string hourString = hour < 10 ? ("0" + hour) : hour.ToString();
        string minuteString = minute < 10 ? ("0" + minute) : minute.ToString();
        string secondString = second < 10 ? ("0" + second) : second.ToString();

        /*根据当前时间的年月日时分秒生成文件名*/
        string fileName = yearString + monthString + dayString + hourString + minuteString + secondString + exeFileString;
        return fileName;
    }

    /*清空设备各个字段的信息*/
    protected void Btn_Cancle_Click(object sender, EventArgs e)
    {
        Response.Write("<script>location.href=location.href;</script>");
    }
}
